<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.docx4j.wml.P" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs2" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs3" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    /**
     * 创建人：邓俊鹏
     * 创建时间： 2021-04-20
     * 功能：我的投标列表数据展示
     */
    bb.writeLog("in----------------mytoubiao.jsp");
    out.println("----------1111111-----------<br>");
    JSONObject jsonObject = new JSONObject();
    String fqpb_formtable = bb.getPropValue("SrmWfTab", "fqpb");
    String gysyq_formtable = bb.getPropValue("SrmWfTab", "gysyq");
    String bsps_formtable = bb.getPropValue("SrmWfTab", "gysbsps");
    String gystb_formtable = bb.getPropValue("SrmWfTab", "gystb");
    String yqhz_formtable = bb.getPropValue("SrmWfTab", "yqhz");
    String zbcg_formtable = bb.getPropValue("SrmWfTab", "zbcglx");
    String fqjc_formtable = bb.getPropValue("SrmWfTab", "gyqfqjc");
    String gmbs_formtable = bb.getPropValue("SrmWfTab", "gmbs");
    String tbbzjjn_formtable = bb.getPropValue("SrmWfTab", "tbbzjjnsq");
    String jcbg_formtable = bb.getPropValue("SrmWfTab", "gysjcbg");
    String view_gys = bb.getPropValue("SrmMdTab", "srmzcgysk");
    String kbjghq_formtable = bb.getPropValue("SrmWfTab", "kbjghq"); // 开标结果会签

    JSONArray array = new JSONArray();
    boolean flag = true;
    String msg = "";
//    【邀请回执】被邀请后，且当前还未到邀请截止日期前，可操作；回执后，按钮不可再操作；
//    【购买标书】如果需要购买标书的，该按钮可操作；否则，不可操作；购买标书后，按钮不可再操作；
//    【缴纳保证金】如果需要收取投标保证金的，该按钮可操作；否则，不可操作；
//    缴纳后，按钮不可再操作；
//    【提疑】在“提疑截止日期”前，可操作；超期后，不可操作；
//    【投标】需要购买标书的，购买后，且未超过“投标截止日期”可操作；否则，不可操作；

    try {
        User user = HrmUserVarify.getUser(request, response);
        int uid = user.getUID();
        out.println("----------uid-----------" + uid + "<br>");

        String sql = "select a.cglxd, e.subcompanyname, a.cglxmc, \n" +
                "case a.cglx\n" +
                "when 0 then '物料'\n" +
                "when 1 then '工程'\n" +
                "when 2 then '租赁'\n" +
                "when 3 then '运输'\n" +
                "when 4 then '服务' end as cglx, a.qrhzjzsj,\n" +
                "(select tbjzrq from " + fqpb_formtable + " where requestid in (select max(requestid) from " + fqpb_formtable + " where cglxd = a.cglxd)) as tbjzrq,\n" +
                "(select tyjzrq from " + fqpb_formtable + " where requestid in (select max(requestid) from " + fqpb_formtable + " where cglxd = a.cglxd)) as tyjzrq\n" +
                "from " + gysyq_formtable + " a\n" +
                "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                "left join " + gysyq_formtable + "_dt6 dt6 on a.id = dt6.mainid\n" +
                "left join " + view_gys + " d on dt6.gysmc = d.id\n" +
                "left join hrmsubcompany e on e.id = a.zbdw\n" +
                "where currentnodetype=3 and d.id = ? \n" +
                "and a.requestid = (select max(requestid) from " + gysyq_formtable + " f where f.cglxd = a.cglxd)\n" +
                "and (b.deleted = 0 or b.deleted is null) \n" +
                "and b.currentnodetype = 3 \n" +
                "order by a.yqrq desc";
        rs.executeQuery(sql, uid);
        bb.writeLog("sql:" + sql);
        bb.writeLog("uid: " + uid);
        while (rs.next()) {
            String showYqhz = "0";
            String showGmbs = "0";
            String showJnbzj = "0";
            String showTy = "0";
            String showTb = "0";
            String showYqhzRequestid = "";  // 邀请回执的requestid
            String showTbRequestid = "";  // 投标打开的requestid
            String xzbm = ""; // 是否参加
            JSONObject json = new JSONObject();
            String cglxd = Util.null2String(rs.getString("cglxd")); // 采购立项单
            String zbdw = Util.null2String(rs.getString("subcompanyname")); // 招标单位
            String cglxmc = Util.null2String(rs.getString("cglxmc")); // 采购立项名称
            String cglx = Util.null2String(rs.getString("cglx")); // 类型
            String qrhzjzsj = Util.null2String(rs.getString("qrhzjzsj")); // 回执截至时间
            String tbjzrq = Util.null2String(rs.getString("tbjzrq")); // 提疑截至时间
            String tyjzrq = Util.null2String(rs.getString("tyjzrq")); // 投标截至时间
            out.println("----------cglxmc-----------" + cglxmc + "<br>");
            // 如果发起评标中没有投标截止时间, 从标书评审中获取
            if ("".equals(tbjzrq)) {
                String tbjzrqSql = "select tbjzsj from " + bsps_formtable + " where cglxd = ?";
                rs2.executeQuery(tbjzrqSql, cglxd);
                if (rs2.next()) {
                    tbjzrq = Util.null2String(rs2.getString("tbjzsj"));
                }
            }

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = new Date();
            String format1 = format.format(date);
            String tbzt = "";

            SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String nowDate = dateTimeFormat.format(new Date());

            SimpleDateFormat dateDayFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowDay = dateDayFormat.format(new Date());


            // 提疑按钮
            if (!"".equals(tyjzrq) && nowDate.compareTo(tyjzrq) < 0) {
                showTy = "1";
            }

            if (!"".equals(qrhzjzsj)) {
                if (format1.compareTo(qrhzjzsj) > 0) {
                    tbzt = "回执截止";
                } else {
                    String dhzSql = "select a.requestid from " + yqhz_formtable + " a\n" +
                            "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                            "left join " + view_gys + " d on a.gysmc = d.id\n" +
                            "where d.id = ? and a.cglxd = ? and b.currentnodetype<>3\n" +
                            "and a.requestid = (select max(requestid) from " + yqhz_formtable + " e where e.cglxd = a.cglxd and e.gysmc = a.gysmc)\n" +
                            "and (b.deleted = 0 or b.deleted is null) \n";
                    rs1.executeQuery(dhzSql, uid, cglxd);
                    bb.writeLog("dhzSql： " + dhzSql);
                    bb.writeLog("uid: " + uid);
                    bb.writeLog("cglxd: " + cglxd);
                    rs1.next();
                    String requestid = Util.null2String(rs1.getString("requestid"));
                    if (!"".equals(requestid)) {
                        tbzt = "待回执";
                        showYqhz = "1";
                        // 查询回执requesid
                        showYqhzRequestid = requestid;
                    }
                }
            }

            if ("".equals(tbzt) || "回执截止".equals(tbzt)) {
                // 是否参加
                String sfcjSql = "select xzbm from " + yqhz_formtable + " a\n" +
                        "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                        "left join " + view_gys + " d on a.gysmc = d.id\n" +
                        "where d.id = ? and a.cglxd = ? and b.currentnodetype=3\n" +
                        "and a.requestid = (select max(requestid) from " + yqhz_formtable + " e where e.cglxd = a.cglxd and e.gysmc = a.gysmc)\n" +
                        "and (b.deleted = 0 or b.deleted is null) \n";
                rs3.executeQuery(sfcjSql, uid, cglxd);

                if (rs3.next()) {
                    xzbm = Util.null2String(rs3.getString("xzbm"));
                }

                if ("0".equals(xzbm)) {
                    // 是否需要缴纳保证金
                    String jnbzjSql = "select tbbzj from " + zbcg_formtable + " where requestid = ?";
                    rs1.executeQuery(jnbzjSql, cglxd);
                    if (rs1.next()) {
                        String tbbzj = Util.null2String(rs1.getString("tbbzj"));
                        if ("0".equals(tbbzj)) {  // 是否需要缴纳保证金 0、需要缴纳保证金
                            showJnbzj = "1";
                        }
                    }

                    if ("1".equals(showJnbzj)) {
                        // 判断是否已经缴纳保证金
                        String sfjnbzjSql = "select count(1) as count from " + tbbzjjn_formtable + " a\n" +
                                "left join " + view_gys + " d on a.gysmc = d.id\n" +
                                "where a.cglxd = ? and d.id = ?";
                        rs1.executeQuery(sfjnbzjSql, cglxd, uid);
                        bb.writeLog("sfjnbzjSql：" + sfjnbzjSql);
                        bb.writeLog("cglxd:" + cglxd);
                        bb.writeLog("uid:" + uid);
                        if (rs1.next()) {
                            String count = Util.null2String(rs1.getString("count"));
                            if (!"0".equals(count)) {
                                showJnbzj = "0";
                            }
                        }
                    }

                    // 判断是否需要购买标书
                    String xygmbsSql = "select xygmbs from " + bsps_formtable + " where cglxd = ?";
                    rs1.executeQuery(xygmbsSql, cglxd);
                    if (rs1.next()) {
                        String xygmbs = Util.null2String(rs1.getString("xygmbs"));
                        // 是否投标
                        String sftbSql = "select max(a.requestid) as requestid from " + gystb_formtable + " a\n" +
                                "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                "left join " + view_gys + " d on a.gysmc = d.id\n" +
                                "where a.cglxd = ? and d.id = ?\n" +
                                "and (b.deleted = 0 or b.deleted is null) \n" +
                                "and b.currentnodetype<>3\n" +
                                "and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)";
                        String sftbCount = "1";
                        rs3.executeQuery(sftbSql, cglxd, uid, cglxd);
                        if (rs3.next()) {
                            showTbRequestid = Util.null2String(rs3.getString("requestid"));
                            if (!"".equals(showTbRequestid)) {
                                sftbCount = "0";
                            }
                        }

                        if ("0".equals(xygmbs)) {  // 需要购买标书
                            showGmbs = "1";
                            if (nowDay.compareTo(tbjzrq) < 0) {
                                // 查询是否购买标书
                                String tbanSql = "select count(1) as count from " + gmbs_formtable + " a\n" +
                                        "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                        "left join " + view_gys + " d on a.gysmc = d.id\n" +
                                        "where currentnodetype=3 and a.cglxd = ? and d.id = ?\n" +
                                        "and (b.deleted = 0 or b.deleted is null) \n";
                                rs2.executeQuery(tbanSql, cglxd, uid);
                                if (rs2.next()) {
                                    String count = Util.null2String(rs2.getString("count"));
                                    if (!"0".equals(count)) {
                                        showGmbs = "0";
                                    }
                                }
                            }
                        }

                        // 未投标
                        if ("0".equals(sftbCount)) {
                            if ("0".equals(showGmbs) && "0".equals(showJnbzj) && nowDay.compareTo(tbjzrq) < 0) {
                                // 显示投标按钮
                                showTb = "1";
                            }
                        }
                    }
                }

            }


//            if ("".equals(tbzt)) {
            String format2 = dateFormat.format(date);
            if (!"".equals(tbjzrq)) {
                out.println("----------tbjzrq-----------" + tbjzrq + "<br>");
                if (!"0".equals(xzbm) && !"".equals(xzbm)) {
                    continue;
                } else if (!"待回执".equals(tbzt)) {
                    out.println("----------tbzt-----------" + tbzt + "<br>");
                    String tbjzSql = "select count(1) as count from " + gystb_formtable + " a\n" +
                            "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                            "left join " + view_gys + " d on a.gysmc = d.id\n" +
                            "where d.id = ? and a.cglxd = ? and b.currentnodetype=3 and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)";
                    rs1.executeQuery(tbjzSql, uid, cglxd, cglxd);
                    bb.writeLog("tbjzSql", tbjzSql);
                    bb.writeLog("cglxd:", cglxd);
                    if (rs1.next()) {
                        String count = Util.null2String(rs1.getString("count"));
                        out.println("----------count-----------" + count + "<br>");
                        if ("0".equals(count) && nowDay.compareTo(tbjzrq) < 0) {
                            tbzt = "待投标";
                        } else if (!"0".equals(count)) {
//                                String xqrsSql = "select zdtbdws from "+zbcg_formtable+" where requestid = ?"; // 需求人数
//                                rs2.executeQuery(xqrsSql, cglxd);
//                                if (rs2.next()) {
//                                    int zdtbdws = Util.getIntValue(Util.null2String(rs2.getString("zdtbdws")));
                            // 投标总数
                            String tbjzzsSql = "select b.currentnodetype from " + gystb_formtable + " a\n" +
                                    "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                    "left join " + view_gys + " d on a.gysmc = d.id\n" +
                                    "where a.cglxd = ? and d.id=? and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)";
                            rs3.executeQuery(tbjzzsSql, cglxd, uid, cglxd);
                            rs3.next();
                            String currentnodetype = Util.null2String(rs3.getString("currentnodetype"));
                            //int countInt = Util.getIntValue(zscount);
                            out.println("----------currentnodetype-----------" + currentnodetype + "<br>");
                            if (!currentnodetype.equals("3")) {
                                tbzt = "投标中";
                            } else {

                                String kbszSql = "select count(1) as count from " + kbjghq_formtable + " a\n" +
                                        "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                        "where  a.cglxd = ? and b.currentnodetype=3 and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)";
                                rs3.executeQuery(kbszSql, cglxd, cglxd);
                                bb.writeLog("kbszSql:", kbszSql);
                                if (rs3.next()) {
                                    String kbcount = Util.null2String(rs3.getString("count"));
                                    if ("0".equals(kbcount)) {
                                        tbzt = "待开标";
                                    } else {
                                        // 判断发起决策
                                        String fqjcSql = "select count(1) as count from " + fqjc_formtable + " a\n" +
                                                "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                                "where  a.cglxd = ? and b.currentnodetype=3 and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)";
                                        rs3.executeQuery(fqjcSql, cglxd, cglxd);
                                        bb.writeLog("fqjcSql:", fqjcSql);
                                        if (rs3.next()) {
                                            String fqjcCount = Util.null2String(rs3.getString("count"));
                                            if ("0".equals(fqjcCount)) {
                                                tbzt = "评标中";
                                            } else {
                                                // 先从中标决策变更中获取中标结果
                                                String zbbgSql = "select count(1) as count, zbjg from " + jcbg_formtable + " a\n" +
                                                        "left join " + jcbg_formtable + "_dt1 dt1 on a.id = dt1.mainid\n" +
                                                        "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                                        "left join " + view_gys + " d on dt1.nzbdw = d.id\n" +
                                                        "where d.id = ? and a.cglxd = ? and b.currentnodetype=3 \n" +
                                                        "and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)\n" +
                                                        "group by zbjg";
                                                rs3.executeQuery(zbbgSql, uid, cglxd, cglxd);
                                                bb.writeLog("zbbgSql:", zbbgSql);
                                                String zbResult = "";
                                                while (rs3.next()) {
                                                    String zbbgCount = Util.null2String(rs3.getString("count"));
                                                    String zbjg = Util.null2String(rs3.getString("zbjg"));

                                                    if (!"0".equals(zbbgCount) && (zbjg.equals("0") || zbjg.equals("1"))) {
                                                        zbResult = "已中标";
                                                        break;
                                                    } else if (!"0".equals(zbbgCount) && (zbjg.equals("2"))) {
                                                        zbResult = "未中标";
                                                        break;
                                                    }
                                                }
                                                tbzt = zbResult;

                                                // 从决策中获取
                                                if (tbzt.equals("")) {
                                                    String fqjczbSql = "select count(1) as count, zbjg from " + fqjc_formtable + " a\n" +
                                                            "left join " + fqjc_formtable + "_dt1 dt1 on a.id = dt1.mainid\n" +
                                                            "left join workflow_requestbase b on b.requestid = a.requestid\n" +
                                                            "left join " + view_gys + " d on dt1.nzbdw = d.id\n" +
                                                            "where d.id = ? and a.cglxd = ? and b.currentnodetype=3 \n" +
                                                            "and a.lc = (select max(lc) from " + fqpb_formtable + " where cglxd = ?)\n" +
                                                            "group by \n" +
                                                            "zbjg";
                                                    rs3.executeQuery(fqjczbSql, uid, cglxd, cglxd);
                                                    bb.writeLog("fqjczbSql:", fqjczbSql);
                                                    while (rs3.next()) {
                                                        String zbbgCount = Util.null2String(rs3.getString("count"));
                                                        String zbjg = Util.null2String(rs3.getString("zbjg"));  // 中标结果
                                                        if (!"0".equals(zbbgCount) && (zbjg.equals("0") || zbjg.equals("1"))) {
                                                            zbResult = "已中标";
                                                            break;
                                                        } else if (!"0".equals(zbbgCount) && (zbjg.equals("2"))) {
                                                            zbResult = "未中标";
                                                            break;
                                                        }
                                                    }
                                                    tbzt = zbResult;
                                                }
                                            }
                                        }


                                    }
                                }
                            }
//                                }
                        } else {
                            // 判断是否流标
                            String lbSql = "select xmzt from " + zbcg_formtable + " where requestid = ?";
                            rs3.executeQuery(lbSql, cglxd);
                            rs3.next();
                            String xmzt = Util.null2String(rs3.getString("xmzt"));
                            if ("2".equals(xmzt)) {
                                tbzt = "已流标";
                            }
                        }
                    }
                }
            }
//            }
            out.println("----------tbzt2-----------" + tbzt + "<br>");
            json.put("cglxd", cglxd);
            json.put("cglxmc", cglxmc);
            json.put("zbdw", zbdw);
            json.put("cglx", cglx);
            json.put("qrhzjzsj", qrhzjzsj);
            json.put("tbjzrq", tbjzrq);
            json.put("tyjzrq", tyjzrq);
            json.put("tbzt", tbzt);
            json.put("showYqhz", showYqhz);
            json.put("showYqhzRequestid", showYqhzRequestid);
            json.put("showGmbs", showGmbs);
            json.put("showJnbzj", showJnbzj);
            json.put("showTy", showTy);
            json.put("showTb", showTb);
            json.put("showTbRequestid", showTbRequestid);
            json.put("uid", uid);
            array.add(json);
        }
    } catch (Exception e) {
        e.printStackTrace();
        flag = false;
        msg = e.getMessage();
    }

//    TODO  1、购买标书流程不对 2、已流标：当前立项已流标的；   招标采购立项 有个字段(未建立)


    jsonObject.put("result", array);
    jsonObject.put("flag", flag);
    jsonObject.put("msg", msg);

    //out.print(jsonObject.toJSONString());
    bb.writeLog("out----------------mytoubiao.jsp" + jsonObject.toJSONString());
%>